#ifndef BHXJNAVIGATIONLIST_H
#define BHXJNAVIGATIONLIST_H

#include <QListView>
#include<QStyledItemDelegate>
#include "BHXJControls_global.h"
#include<QPainter>
#include<QKeyEvent>
#include<QMouseEvent>
#include<QStandardItem>
#include<QStandardItemModel>
#include<QSortFilterProxyModel>
#include<QVariant>

class BHXJCONTROLS_EXPORT BHXJNavTreeNode:public QObject
{
    Q_OBJECT
public:
    explicit BHXJNavTreeNode(QObject *parent=nullptr);
    //virtual ~BHXJNavTreeNode();

    BHXJNavTreeNode * createNode(QString text);

    int level=0;
    bool expand=false;
    QString text;
    BHXJNavTreeNode* parent=nullptr;
    QList<BHXJNavTreeNode*> nodes;

    QStandardItem *item=nullptr;
signals:
    void clicked();
};

class BHXJCONTROLS_EXPORT BHXJNavItemDelegate : public QStyledItemDelegate
{
    Q_OBJECT

public:
    BHXJNavItemDelegate(QWidget *parent = nullptr);

    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    QSize sizeHint(const QStyleOptionViewItem &option,const QModelIndex &index) const override;
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const override;
    void setEditorData(QWidget *editor, const QModelIndex &index) const override;
    void setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const override;
private slots:

};

class BHXJCONTROLS_EXPORT BHXJNavProxyModel: public QSortFilterProxyModel
{
public:
    explicit BHXJNavProxyModel(QObject *parent=nullptr);

    bool filterAcceptsRow ( int source_row,const QModelIndex & source_parent )const override;

protected:
    bool getexpland(const BHXJNavTreeNode * node)const
    {
        if(node->parent!=nullptr)
        {
            if(node->parent->expand)
            {
                return getexpland(node->parent);
            }
            else
            {
                return false;
            }

        }
        else
        {
            return true;
        }
    }
};

class BHXJCONTROLS_EXPORT BHXJNavigation : public QListView
{
    Q_OBJECT
public:
    explicit BHXJNavigation(QWidget *parent = nullptr);


    BHXJNavTreeNode* addNode(QString text);
    BHXJNavTreeNode* addNode(QString text,BHXJNavTreeNode* parent);
    void removeNode(BHXJNavTreeNode* node);

    void explandAll();
    void explandAll(BHXJNavTreeNode* node,bool upda=true);
signals:

protected:
    BHXJNavTreeNode*   node;
    QStandardItemModel *itemmodel;
    BHXJNavProxyModel *proxymodel;
    BHXJNavItemDelegate *delegate;

    int findLastIndex(BHXJNavTreeNode *node);

    void onClicked(const QModelIndex &index);
    void onDoubleClicked(const QModelIndex &index);

public slots:
};

#endif // BHXJNAVIGATIONLIST_H
